package tablet.module_user.activity;

import android.content.DialogInterface;
import android.view.View;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.app.EventBusTagUtils;
import com.lib.bean.Event;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.db.shared_prefrences.interfaces.UserCacheInterface;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.utils.string_deal.regex.RegexUtils;
import com.lib.fastkit.views.dialog.normal.CoachAIStudyDialog;
import com.lib.fastkit.views.dialog.zenhui.AlertDialog;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.activity.BaseAppActivity;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import tablet.module_user.R;
import tablet.module_user.R2;
import tablet.module_user.bean.BaseLoginBean;
import tablet.module_user.bean.IdentityUserBean;
import tablet.module_user.fragment.StudentChooseFragment;
import tablet.module_user.fragment.StudentXieYiFragment;
import tablet.module_user.fragment.TeacherChooseFragment;

@Route(path = ARouterPathUtils.Tablet_User_IdentityActivity)
public class IdentityActivity extends BaseAppActivity {

    @BindView(R2.id.f_teacher)
    FrameLayout fTeacher;
    @BindView(R2.id.f_student)
    FrameLayout fStudent;

    @Override
    protected void onCreateView() {

        ButterKnife.bind(this);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_identity;
    }


    private TeacherChooseFragment teacherChooseFragment;

    private StudentChooseFragment studentChooseFragment;

    @OnClick({R2.id.f_teacher, R2.id.f_student})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.f_teacher) {

            CoachAIStudyDialog.getInstance()
                    .setTitle("溫馨提示")
                    .setContent("身份选定之后将不能更改，是否确定您选择的老师身份？")
                    .setSureListener(new CoachAIStudyDialog.SurelListener() {
                        @Override
                        public void onSure() {
                            showTeacher();
                        }
                    }).show(getSupportFragmentManager());


        } else if (i == R.id.f_student) {

            CoachAIStudyDialog.getInstance()
                    .setTitle("溫馨提示")
                    .setContent("身份选定之后将不能更改，是否确定您选择的学生身份？")
                    .setSureListener(new CoachAIStudyDialog.SurelListener() {
                        @Override
                        public void onSure() {
                            showStudentXieYi();
                        }
                    }).show(getSupportFragmentManager());


        }
    }

    StudentXieYiFragment studentXieYiFragment;

    private void showStudentXieYi() {

        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setContentView(R.layout.dialog_student_xieyi)
                .setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (studentXieYiFragment != null) {
                            getSupportFragmentManager().beginTransaction().remove(studentXieYiFragment).commit();
                            studentXieYiFragment = null;
                        }
                    }
                })
                .fullWidth()
                .addDefaultAnimation()
                .show();

        studentXieYiFragment = (StudentXieYiFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_studenxieyi);

        studentXieYiFragment.setStudentClickListener(new StudentXieYiFragment.StudentClickListener() {
            @Override
            public void clickCancel() {
                dialog.dismiss();

//                ARouter.getInstance().build(ARouterPathUtils.User_LoginActivity).navigation();
                IdentityActivity.this.finish();

            }

            @Override
            public void clickConfirm() {

                dialog.dismiss();
                showStudent();

            }
        });

    }

    private void showStudent() {
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setContentView(R.layout.dialog_student)
                .setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (studentChooseFragment != null) {
                            getSupportFragmentManager().beginTransaction().remove(studentChooseFragment).commit();
                            studentChooseFragment = null;
                        }
                    }
                })

                .fullWidth()
                .addDefaultAnimation()
                .show();

        studentChooseFragment = (StudentChooseFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_teacher);
        studentChooseFragment.setGradeChooseListener(new StudentChooseFragment.GradeChooseListener() {
            @Override
            public void onSure() {
                ARouter.getInstance().build(ARouterPathUtils.Tablet_User_StartActivity).navigation();
                dialog.dismiss();
            }

            @Override
            public void onGradeChoose(String str) {
                //showToast(str);
            }
        });

    }

    private void showTeacher() {
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setContentView(R.layout.dialog_teacher)
                .setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {

                        if (teacherChooseFragment != null) {
                            getSupportFragmentManager().beginTransaction().remove(teacherChooseFragment).commit();
                        }


                    }
                })

                .fullWidth()
                .addDefaultAnimation()
                .show();


        teacherChooseFragment = (TeacherChooseFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_teacher);
        teacherChooseFragment.setGradeChooseListener(new TeacherChooseFragment.GradeChooseListener() {
            @Override
            public void onSure() {

                ARouter.getInstance().build(ARouterPathUtils.Tablet_User_StartActivity).navigation();

                dialog.dismiss();

            }

            @Override
            public void onGradeChoose(String str) {
                //showToast(str);


            }
        });

    }


    @Subscriber(tag = EventBusTagUtils.TeacherStudentChooseFragment)
    public void fromTeacherStudentChooseFragment(Event event) {
        switch (event.getEventCode()) {
            case 1: {

                LogUtil.e("用户选择科目/年级成功了，在帮TA注册一下");

                IdentityUserBean identityUserBean = (IdentityUserBean) event.getData();

                requestRegist(identityUserBean);


                break;
            }

        }

    }


    private void requestRegist(IdentityUserBean identityUserBean) {

        String phone = SharedPreferenceManager.getInstance(this).getUserCache().getUserPhone();
        if (!RegexUtils.checkPhone(this, phone)) {

            return;
        }

        HttpUtils.with(this)
                .addParam("requestType", "REGISTERED")
                .addParam("phone", phone)
                .addParam("identity", identityUserBean.getType())
                .addParam("grade_id", identityUserBean.getGrade_id())
                .addParam("subject_id", identityUserBean.getSubject_id())
                .execute(new HttpNormalCallBack<BaseLoginBean>() {
                    @Override
                    public void onSuccess(BaseLoginBean result) {

                        if (result.getCode() == CodeUtil.CODE_200) {

                            LogUtil.e("===========注册成功==============");

                            UserCacheInterface userCacheInterface = SharedPreferenceManager.getInstance(IdentityActivity.this).getUserCache();
                            userCacheInterface.setUserToken(result.getObj().getToken());
                            userCacheInterface.setUserIdentity(result.getObj().getIdentity());
                            userCacheInterface.setUserName(result.getObj().getName());
                            userCacheInterface.setUserHeadUrl(result.getObj().getPhoto_url());
                            userCacheInterface.setUserPhone(result.getObj().getPhone());

                            finish();

                        }

                        showToast(result.getMsg());

                    }

                    @Override
                    public void onError(String e) {

                    }
                });


    }


}
